package com.fqcar.fqdf.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.entity.HistoryCarListBean;
import com.fqcar.fqdf.plugin.image.ImageZoomActivity;
import com.fqcar.fqdf.plugin.image.ImageZoomBean;
import com.fqdf.framework.android.adapter.BaseMyAdapter;
import com.fqdf.framework.android.util.AndroidUtils;
import com.fqdf.framework.android.util.CheckLogicUtil;
import com.fqdf.framework.android.util.Constants;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryCarListAdapter extends BaseMyAdapter<HistoryCarListBean> {


    private Context context;

    private HistoryImgListAdapter historyImgListAdapter;

    public HistoryCarListAdapter(Context context) {
        super(context);
        this.context = context;
    }
    /*public HistoryCarListAdapter(Context context,HistoryImgListAdapter historyImgListAdapter) {
        super(context);
        this.context = context;
        this.historyImgListAdapter = historyImgListAdapter;
    }*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.adapter_car_history_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        historyImgListAdapter = new HistoryImgListAdapter(context);
//        holder.detailListImg.setAdapter(historyImgListAdapter);
//        historyImgListAdapter.notifyDataSetChanged();
        final HistoryCarListBean historyCarListBean = getItem(position);
//        holder.historyManagerId.setText(historyCarListBean.getId() + "");
        holder.historyOperation.setText(context.getString(R.string.manage_name_txt) + historyCarListBean.getAdminName());

        if (!CheckLogicUtil.isEmpty(historyCarListBean.getCause()) && !CheckLogicUtil.isEmpty(historyCarListBean.getDetail())) {
            holder.historyCause.setText(historyCarListBean.getCause() + "," + historyCarListBean.getDetail());
        } else if (!CheckLogicUtil.isEmpty(historyCarListBean.getDetail())) {
            holder.historyCause.setText(historyCarListBean.getDetail());
        } else if (!CheckLogicUtil.isEmpty(historyCarListBean.getCause())) {
            holder.historyCause.setText(historyCarListBean.getCause());
        } else if (CheckLogicUtil.isEmpty(historyCarListBean.getCause()) && CheckLogicUtil.isEmpty(historyCarListBean.getDetail())) {
            holder.historyCause.setText("");
        }


        holder.history_cause_time.setText(Html.fromHtml(historyCarListBean.getEndTimeDesc()));
        /*if (!CheckLogicUtil.isEmpty(historyCarListBean.getEndTime())) {
            holder.history_cause_time.setText(historyCarListBean.getEndTime());
            holder.history_cause_time.setTextColor(AndroidUtils.getColor(context, R.color.black));
        } else {
            holder.history_cause_time.setText(context.getString(R.string.manage_doing_txt));
            holder.history_cause_time.setTextColor(AndroidUtils.getColor(context, R.color.nf26050));
        }*/

        holder.historyDetailImg.setVisibility(historyCarListBean.getDealImgList().size() > 0 ? View.VISIBLE : View.GONE);
        if(historyCarListBean.getDealImgList().size() > 0){
            Glide.with(context).load(historyCarListBean.getDealImgList().get(0).getImgUrl()).into(holder.historyDetailImg);
        }

        holder.historyDetailImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentl = new Intent();
                intentl.setClass(context, ImageZoomActivity.class);
                Bundle bundle = new Bundle();
                ArrayList<ImageZoomBean> imageZoomList = new ArrayList<>();
                for (int i = 0; i < historyCarListBean.getDealImgList().size(); i++) {
                    imageZoomList.add(new ImageZoomBean(historyCarListBean.getDealImgList().get(i).getImgUrl()));
                }

                bundle.putParcelableArrayList(Constants.BUNDLE_DATA, imageZoomList);
                intentl.putExtras(bundle);
                context.startActivity(intentl);
            }
        });
        holder.callBtn.setVisibility(CheckLogicUtil.isEmpty(historyCarListBean.getSim()) ? View.GONE : View.VISIBLE);
//        holder.callBtn.setText(historyCarListBean.getAdminName());
        holder.callBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(AndroidUtils.getTelIntent(historyCarListBean.getSim()));
            }
        });

        return convertView;
    }

    static class ViewHolder {

//        @BindView(R.id.history_manager_id)
//        TextView historyManagerId;
        @BindView(R.id.history_operation)
        TextView historyOperation;
        @BindView(R.id.history_cause)
        TextView historyCause;
        @BindView(R.id.history_cause_time)
        TextView history_cause_time;
        @BindView(R.id.history_detail_img)
        ImageView historyDetailImg;
        @BindView(R.id.call_btn)
        Button callBtn;
//        @BindView(R.id.detail_list_img)
//        RecyclerView detailListImg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
